import { Injectable } from '@angular/core';
import {
  CanActivate,
  Router,
  ActivatedRouteSnapshot,
  RouterStateSnapshot
} from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private router: Router) { }
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    const url: string = state.url;
    const userinfo = window.localStorage.getItem('userinfo');
    if (userinfo || url === '/users/login') {
      return true;
    } else {
      this.router.navigate(['/users/login']);
    }
  }
}
